package com.antt.cn.test2.uis.activities;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.antt.cn.test2.R;
import com.antt.cn.test2.http.common.UrlCommon;
import com.antt.cn.test2.http.retrofit.BaseResponse;
import com.antt.cn.test2.uis.base.BaseActivity;
import com.antt.cn.test2.utils.AntLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
 * Created by baichuan on 2017/4/21.
 */

public class Test01 extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.ac_test01_button)
    Button obt;
    @BindView(R.id.ac_test01_textview)
    TextView text;

    @Override
    public int getLayoutId() {
        return R.layout.ac_test01_layout;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);

    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        obt.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ac_test01_button:
//                getData();
                getData1();
                break;
        }
    }

    public void getData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlCommon.ROOT)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserLogin userLogin = retrofit.create(UserLogin.class);
        Call<BaseResponse> call = userLogin.getData("11");
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                AntLog.i("response==" + response.body());
                text.setText(response.body().getMessage());
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {

            }
        });

    }

    public void getData1() {
        AntLog.i("getData1()");
        new Retrofit.Builder()
                .baseUrl("http://10.1.1.10:9002/v1/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(UserLogin1.class)
                .getData(20)
                .enqueue(new Callback<String>() {

                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        AntLog.i("body=" + response.body());
                        text.setText(response.body());
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        AntLog.i(TAG, "onError");
                    }
                });
    }

    public interface UserLogin1 {
        @GET("book.category.list")
        Call<String> getData(@Query("pageSize") int size);
    }

    public interface UserLogin {
        @GET(UrlCommon.BOOK_USER_LOGIN)
        Call<BaseResponse> getData(@Query("id") String id);
    }
}
